//package com.smile.config;
//
//import co.elastic.clients.elasticsearch.ElasticsearchClient;
//import co.elastic.clients.json.jackson.JacksonJsonpMapper;
//import co.elastic.clients.transport.ElasticsearchTransport;
//import co.elastic.clients.transport.rest_client.RestClientTransport;
//import org.apache.http.HttpHost;
//import org.apache.http.auth.AuthScope;
//import org.apache.http.auth.UsernamePasswordCredentials;
//import org.apache.http.client.CredentialsProvider;
//import org.apache.http.conn.ssl.NoopHostnameVerifier;
//import org.apache.http.impl.client.BasicCredentialsProvider;
//import org.apache.http.ssl.SSLContexts;
//import org.elasticsearch.client.RestClient;
//import org.elasticsearch.client.RestClientBuilder;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Primary;
//
//import javax.net.ssl.SSLContext;
//import javax.net.ssl.TrustManager;
//import javax.net.ssl.X509TrustManager;
//import java.security.KeyManagementException;
//import java.security.KeyStoreException;
//import java.security.NoSuchAlgorithmException;
//import java.security.cert.X509Certificate;
//
//@Configuration
//public class ElasticsearchConfig {
//
//    // 全局禁用SSL证书和主机名校验（仅开发环境使用！）
//    static {
//        try {
//            TrustManager[] trustAllCerts = new TrustManager[]{
//                    new X509TrustManager() {
//                        public void checkClientTrusted(X509Certificate[] chain, String authType) {}
//                        public void checkServerTrusted(X509Certificate[] chain, String authType) {}
//                        public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
//                    }
//            };
//            SSLContext sc = SSLContext.getInstance("TLS");
//            sc.init(null, trustAllCerts, new java.security.SecureRandom());
//            javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
//            javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    @Value("${spring.elasticsearch.rest.username}")
//    private String esUsername;
//
//    @Value("${spring.elasticsearch.rest.password}")
//    private String esPassword;
//
//    @Bean
//    public ElasticsearchClient elasticsearchClient()
//            throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
//
//        // 创建信任所有证书的SSL上下文
//        SSLContext sslContext = SSLContexts.custom()
//                .loadTrustMaterial(null, (chain, authType) -> true)
//                .build();
//
//        // 创建认证凭证提供器
//        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//        credentialsProvider.setCredentials(AuthScope.ANY,
//                new UsernamePasswordCredentials(esUsername, esPassword));
//
//        // 构建RestClient并应用安全配置
//        RestClient restClient = RestClient.builder(
//                new HttpHost("192.168.0.80", 10100, "https")
//        ).setHttpClientConfigCallback(httpClientBuilder -> {
//            return httpClientBuilder
//                    .setSSLContext(sslContext)
//                    .setSSLHostnameVerifier(new NoopHostnameVerifier())
//                    .setDefaultCredentialsProvider(credentialsProvider);
//        }).build();
//
//        // 创建传输层
//        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
//
//        return new ElasticsearchClient(transport);
//    }
//
//    @Bean
//    public org.elasticsearch.client.RestHighLevelClient restHighLevelClient() throws Exception {
//        SSLContext sslContext = SSLContexts.custom()
//                .loadTrustMaterial(null, (chain, authType) -> true)
//                .build();
//
//        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//        credentialsProvider.setCredentials(AuthScope.ANY,
//                new UsernamePasswordCredentials(esUsername, esPassword));
//
//        RestClientBuilder builder = RestClient.builder(
//                        new HttpHost("192.168.0.80", 10100, "https"))
//                .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
//                        .setSSLContext(sslContext)
//                        .setSSLHostnameVerifier(new NoopHostnameVerifier())
//                        .setDefaultCredentialsProvider(credentialsProvider)
//                );
//        return new org.elasticsearch.client.RestHighLevelClient(builder);
//    }
//
//    @Bean
//    public org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate elasticsearchRestTemplate(org.elasticsearch.client.RestHighLevelClient client) {
//        return new org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate(client);
//    }
//
//    @Bean("elasticsearchTemplate")
//    @Primary
//    public org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate elasticsearchTemplate(org.elasticsearch.client.RestHighLevelClient client) {
//        return new org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate(client);
//    }
//
//    /**
//     * 用于测试SSL证书问题的独立方法
//     * 调用此方法可验证SSLContext配置是否正确
//     */
//    public void testSSLConfiguration() {
//        try {
//            SSLContext testContext = SSLContexts.custom()
//                    .loadTrustMaterial(null, (chain, authType) -> true)
//                    .build();
//            System.out.println("成功创建信任所有证书的SSL上下文");
//        } catch (Exception e) {
//            e.printStackTrace();
//            System.err.println("SSL配置失败: " + e.getMessage());
//        }
//    }
//}